/*
 * Copyright (c) 2004 Your Corporation. All Rights Reserved.
 */
package com.talios.jira.browser;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.PsiElement;
import com.intellij.usageView.UsageInfo;
import com.intellij.usageView.UsageViewDescriptor;

import java.text.MessageFormat;
import java.util.Set;

public class IssuesUsageViewDescriptor implements UsageViewDescriptor {
    private Set issueReferences;
    private JiraItem issue;
    private IssueSearcher issueSearcher;

    public IssuesUsageViewDescriptor(JiraItem issue, IssueSearcher issueSearcher) {
        this.issue = issue;
        this.issueSearcher = issueSearcher;

        issueSearcher.run();
        this.issueReferences = issueSearcher.getIssueReferences();
    }

    public Set getIssueReferences() {
        return issueReferences;
    }

    public PsiElement[] getElements() {
        return new PsiElement[0];
    }

    public UsageInfo[] getUsages() {
        return (UsageInfo[]) issueReferences.toArray(new UsageInfo[]{});
    }

    public void refresh(PsiElement[] psiElements) {
        ApplicationManager.getApplication().runReadAction(issueSearcher);
        this.issueReferences = issueSearcher.getIssueReferences();
    }

    public String getProcessedElementsHeader() {
        String format = "Occurences of {0}";
        return MessageFormat.format(format, new Object[]{issue.getKey()});
    }

    public boolean isSearchInText() {
        return false;
    }

    public boolean toMarkInvalidOrReadonlyUsages() {
        return false;
    }

    public String getCodeReferencesText(int i, int i1) {
        String format = "{0}: {1} ( {2} occurrences in {3} files )";
        return MessageFormat.format(format, new Object[]{issue.getKey(), issue.getTitle(), String.valueOf(i), String.valueOf(i1)});
    }

    public String getCodeReferencesWord() {
        return "issue reference";
    }

    public String getCommentReferencesText(int i, int i1) {
        return "getCommentReferencesText";
    }

    public String getCommentReferencesWord() {
        return "getCommentReferencesWord";
    }

    public boolean cancelAvailable() {
        return false;
    }

    public boolean isCancelInCommonGroup() {
        return false;
    }

    public boolean canRefresh() {
        return true;
    }

    public boolean willUsageBeChanged(UsageInfo usageInfo) {
        return false;
    }

    public String getHelpID() {
        return null;
    }

    public boolean canFilterMethods() {
        return true;
    }
}